<?php
function db_get_link(){
	$link = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD) or die('could not connect');
	mysql_select_db(DATABASE, $link);
	return $link;
}

function db_query($sql, $args = null){
	if (!@is_array($args)) {
		$args = func_get_args();
		array_shift($args);
	}
	$link = db_get_link();
	foreach($args as $i => $param){
		if(!is_numeric($param)){
			$args[$i] = mysql_real_escape_string($param);
		}

	}
	$sql = vsprintf($sql, $args);
//	print "<p>$sql</p>	";

	$result = mysql_query($sql,$link) or die('error: ' . mysql_error());
	return $result;
}

function db_insert_id_query($sql, $args = null){

	if (!@is_array($args)) {
		$args = func_get_args();
		array_shift($args);
	}
	$link = db_get_link();
	foreach($args as $i => $param){
		if(!is_numeric($param)){
			$args[$i] = mysql_real_escape_string($param);
		}

	}

	$sql = vsprintf($sql, $args);


	mysql_query($sql,$link) or die('error: ' . mysql_error());
	return mysql_insert_id($link);
}

function db_scalar_query($sql, $args = null){
	$result = db_query($sql,$args);
	if($result){
		return mysql_result($result,0);
	}
	return false;
}

